<template>
	<view class="goods-manage">
		<u-sticky>
			<u-tabs :list="tabList" @click="click" @change="tabsChange"></u-tabs>
		</u-sticky>

		<swiper :current="swiperCurrent">
			<!-- 出售中 -->
			<swiper-item class="swiper-item for-sale">
				<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="onreachBottom">
					<view class="sort-box">
						<block v-for="(orderItem,index) in orderFields" :key="index">
							<view class="sort-item" :class="{'black':orderIndex == index}" @tap="handleOrderField(index)">
								<text>{{ orderItem.name }}</text>
								<view class="arrow-box">
									<u-icon name="arrow-up" size="14" :color="orderIndex==index&&sortType=='asc'?'#333':'#eee'"></u-icon>
									<u-icon name="arrow-down" size="14" :color="orderIndex==index&&sortType=='desc'?'#333':'#eee'"></u-icon>
								</view>
							</view>
						</block>
					</view>
					<view class="sale-list" v-for="(goodsItem,index) in goodsList" :key="index">
						<view class="sale-item">
							<view class="sale-item-head">
								<view class="sale-image">
									<image class="image" :src="goodsItem.image"></image>
								</view>
								<view class="sale-info">
									<view class="sale-title">{{ goodsItem.title }}</view>
									<view class="sale-detail">
										<view class="sale-detail-price">
											<text>￥{{ goodsItem.price }}</text>
										</view>
										<view class="detail-first">
											<view class="total-sales detail-item">
												<text>总销量 {{ goodsItem.sales }}</text>
											</view>
											<view class="stock detail-item">
												<text>库存 {{ goodsItem.stock }}</text>
											</view>	
										</view>
										<view class="detail-last">
											<view class="add-time detail-item">
												<text>添加时间 {{ goodsItem.created_at }}</text>
											</view>
										</view>
									</view>
									
								</view>
								<view class="arrow-right">
									<u-icon name="arrow-right" color="#eee"></u-icon>
								</view>
							</view>
							<view class="sale-operator">
								<view class="sale-operator-item">
									<text @click="goodsUpDown('down', goodsItem.id)">下架</text>
								</view>
								<view class="sale-operator-item">
									<text>分享</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
			<!-- 已下架 -->
			<swiper-item class="swiper-item for-sale">
				<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="onreachBottom">
					<view class="sort-box">
						<block v-for="(orderItem,index) in orderFields" :key="index">
							<view class="sort-item" :class="{'black':orderIndex == index}" @tap="handleOrderField(index)">
								<text>{{ orderItem.name }}</text>
								<view class="arrow-box">
									<u-icon name="arrow-up" size="14" :color="orderIndex==index&&sortType=='asc'?'#333':'#eee'"></u-icon>
									<u-icon name="arrow-down" size="14" :color="orderIndex==index&&sortType=='desc'?'#333':'#eee'"></u-icon>
								</view>
							</view>
						</block>
					</view>
					<view class="sale-list" v-for="(goodsItem,index) in goodsList" :key="index">
						<view class="sale-item">
							<view class="sale-item-head">
								<view class="sale-image">
									<image class="image" :src="goodsItem.image"></image>
								</view>
								<view class="sale-info">
									<view class="sale-title">{{ goodsItem.title }}</view>
									<view class="sale-detail">
										<view class="sale-detail-price">
											<text>￥{{ goodsItem.price }}</text>
										</view>
										<view class="detail-first">
											<view class="total-sales detail-item">
												<text>总销量 {{ goodsItem.sales }}</text>
											</view>
											<view class="stock detail-item">
												<text>库存 {{ goodsItem.stock }}</text>
											</view>	
										</view>
										<view class="detail-last">
											<view class="add-time detail-item">
												<text>添加时间 {{ goodsItem.created_at }}</text>
											</view>
										</view>
									</view>
									
								</view>
								<view class="arrow-right">
									<u-icon name="arrow-right" color="#eee"></u-icon>
								</view>
							</view>
							<view class="sale-operator">
								<view class="sale-operator-item">
									<text @click="goodsUpDown('up', goodsItem.id)">上架</text>
								</view>
								<view class="sale-operator-item">
									<text>分享</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	export default {
		data() {
			return {
				tabList: [
					{name: '销售中', count: 0},
					{name: '已下架', count: 0},
				],
				swiperCurrent: 0, // 当前活动的swiper-item
				isIndex0Active: true,
				orderFields: [
					{id: 'created_at', name: '添加时间'},
					{id: 'sales', name: '总销量'},
					{id: 'stock', name: '库存'}
				],
				orderIndex: 0,
				sortType: 'asc',
				goodsList: []
			}
		},
		computed: {
			...mapState({
				userInfo: ({ user }) => user.userInfo,
			})
		},
		onLoad() {
			this.getGoodsList()
		},
		methods: {
			tabsChange(index) {
				this.swiperCurrent = index
				this.orderIndex = 0
				this.sortType = 'asc'
				this.goodsList = []
				this.getGoodsList()
			},
			transition(e) {
				let dx = e.detail.dx;
				this.$refs.uTabs.setDx(dx);
			},
			handleOrderField(index) {
				if (this.orderIndex == index) {
					if (this.sortType == 'asc') {
						this.sortType = 'desc'
					} else {
						this.sortType = 'asc'
					}
				} else {
					this.sortType = 'asc'
				}
				this.orderIndex = index
				this.getGoodsList()
			},
			getGoodsList() {
				let that = this
				that.$http('dianpu.goods', {
				    dianpu_id: that.userInfo.dianpu.id,
					type: that.swiperCurrent == 0 ? 'up' : 'down',
					order: that.orderFields[that.orderIndex].id,
					sort: that.sortType
				}).then(res => {
					if (res.code == 1) {
						that.goodsList = res.data
						that.tabList[that.swiperCurrent].count = that.goodsList.length
					}
				})
			},
			goodsUpDown(type, goods_id) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要' + (type=='up'?'上架':'下架') + '吗？',
					success: function (res) {
						if (res.confirm) {
							that.$http('dianpu.goodsupdown', {
								type: type,
							    goods_id: goods_id,
							}).then(res => {
								if (res.code == 1) {
									that.getGoodsList()
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	$border-color:#eee;

	uni-swiper {
		height: calc(100vh - 40px - 44px); // 减去导航和tab栏的高度
	}

	.black {
		color: #333;
	}

	page {
		background-color: #fff;
	}

	.sort-box {
		background-color: #fff;
		display: flex;
		justify-content: space-around;
		padding: 20rpx;
		border-bottom: 1px solid $border-color;
		margin-top: -7rpx;
		color: #ccc;

		.sort-item {
			width: 28%;
			padding: 0 28rpx;
			display: flex;

			.arrow-box {
				display: flex;
				flex-direction: column;
				margin-left: 10rpx;
				justify-content: center;
				position: relative;

				& /deep/ .u-icon:first-child {
					position: absolute;
					top: 4rpx;
				}

				& /deep/ .u-icon:last-child {
					position: absolute;
					bottom: -4rpx;
				}
			}
		}
	}

	.sale-list {
		padding: 20rpx;
		.sale-item-head{
			display: flex;
		}
		.sale-item {
			margin-bottom: 20rpx;
			.sale-info{
				width: 73%;
				margin-left: 20rpx;
				.sale-title{
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
				
				.sale-detail{
					font-size: 24rpx;
					.sale-detail-price{
						color: #c73329;
					}
					.detail-first,.detail-last{
						display: flex;
						margin-top: 10rpx;
						color: #c7c7c7;
					}
					.detail-item{
						width: 60%;
					}
				}
			}
			.arrow-right{
				display: flex;
				flex-direction: column;
				justify-content: center;
			}
		
	
			.sale-image {
				width: 160rpx;
				height: 160rpx;
	
				.image {
					width: 160rpx;
					height: 160rpx;
				}
			}
			padding-bottom: 30rpx;
			&:not(:last-child){
				border-bottom: 1px solid $border-color;
			}
			
			.sale-operator{
				margin-top: 30rpx;
				border: 1px solid $border-color;
				height: 60rpx;
				display: flex;
				align-items: center;
				font-size: 28rpx;
				.sale-operator-item{
					flex: 1;
					text-align: center;
					&:first-child{
						border-right: 1px solid $border-color;
					}
				}
			}
		}
	}
</style>
